<?php

/**
 * Class AccountController
 */
class ShopUserController extends BaseController
{

    public function __construct(){
        parent::__construct();
//权限控制
        $info = session_get(UserEnum::USER_INFO_SESSION_KEY);
        if(empty($info)){

            echo "<script>alert('你还没有登录,请重新登陆');location='/login';</script>";

//      echo"<script>Showbo.Msg.nameauth('你还没有登录,请重新登陆',function(){
            //      location='/login';
            // })</script>";

        }
    }


#—————————————————————————————个人中心——————————————————————————————
    /**
     * @return mixed
     * 账户信息
     */
    public function UserAccount(){
        $token = QiniuUpload::instance()->generateToken();
        return $this->view("shop.shop_user_account")->with('token',$token);
    }

    /**
     * @return mixed
     * 账户信息修改
     */
    public function UserAccountModify(){

            #接受参数
            $paramObj                     =          new stdClass();
            $paramObj->phone              =          $this->getParam('phone');         //联系电话
            $paramObj->user_name          =          $this->getParam('user_name');         //用户名
            $paramObj->corporation        =          $this->getParam('corporation');             //公司名
            $paramObj->address            =          $this->getParam('address');             //地址
            $paramObj->avatar              =          $this->getParam('avatar');             //头像
            #逻辑处理
            try{
                return   ShopService::instance()->UserAccountModify($paramObj);
            }catch (Exception $e){
                print $e->getMessage();
            }

    }



    /**
     * @return mixed
     * 现货添加
     */
    public function UserNowResource(){
        if(session_get(UserEnum::USER_INFO_SESSION_KEY)->is_supplier){
            return $this->view("shop.shop_user_now_resource");
        }else{
            return Redirect::to('/user_seller_join');
        }
    }


    /**
     * @return mixed
     * 现货入库
     */
    public function UserAddNowResource(){
            #收集信息
            $paramObj                   =          new stdClass();
            $paramObj->product_name     =          $this->getParam('product_name');          //产品名称
            $paramObj->spec             =          $this->getParam('spec');                  //规格
            $paramObj->place            =          $this->getParam('place');                 //产地
            $paramObj->storage          =          $this->getParam('storage');               //仓储地
            $paramObj->level            =          $this->getParam('level');                 //等级
            $paramObj->stock            =          $this->getParam('stock');                 //库存量
            $paramObj->contact          =          $this->getParam('contact');               //联系方式
            $paramObj->contact_name     =          $this->getParam('contact_name');          //联系人
            $paramObj->start_num        =          $this->getParam('start_num');             //起订量
            $paramObj->product_img      =          $this->getParam('product_img');           //商品图片
            $paramObj->price            =          $this->getParam('price');                 //商品价格
            $paramObj->unit             =          $this->getParam('unit');                  //单位
            $paramObj->show_img1        =          $this->getParam('show_img1');             //展示图1
            $paramObj->show_img2        =          $this->getParam('show_img2');             //展示图2
            $paramObj->description      =          $this->getParam('description');           //商品描述
            $paramObj->address          =          $this->getParam('address');               //详细地址
            $paramObj->company          =          $this->getParam('company');               //公司
            #逻辑处理

                $re = ShopService::instance()->UserAddNowResource($paramObj);
                if($re){
                    return Redirect::to("/user_now_resource");
                }

    }


    /**
     * @return mixed
     * 现货管理
     */
    public function UserNowResourceAdmin(){
        if(session_get(UserEnum::USER_INFO_SESSION_KEY)->is_supplier){
            $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            $list = ShopService::instance()->UserNowResourceAdmin($user_id);
            return $this->view("shop.shop_user_now_resource_admin")->with('list',$list);
        }else{
            return Redirect::to('/user_seller_join');
        }
    }

    /**
     * @return mixed
     * 现货删除
     */
    public function NowResourceDelete(){
        #接受参数
        $paramObj                     =          new stdClass();
        $paramObj->product_id           =          $this->getParam('product_id');         //现货ID
        $list = ShopService::instance()->NowResourceDelete($paramObj);
        $this->rest->success(['state' => $list], ErrorCodeEnum::STATUS_SUCCESS, '刪除成功！');
    }

    /**
     * 现货删除ajax返回模板
     * @return mixed
     * @author fyl
     * @date 2016-06-29
     */
    public function NowResourceAjax()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $info = ShopService::instance()->UserNowResourceAdmin($user_id);
        return $this->viewAjax('shop.ajaxTemplates.new_resource_part')->with('list', $info);
    }

    /**
     * @return mixed
     * 个人中心发布采购单
     */
    public function UserPurchaseResource(){
        $user_id =session_get(UserEnum::USER_INFO_USER_ID);   //user_id
        #获取用户信息
        $user = ShopService::instance()->IndexUser($user_id);
        return $this->view("shop.shop_user_purchase_resource")->with(['user'=>$user]);
    }

    /**
     * @return mixed
     * 个人中心采购单管理
     */

    public function UserPurchaseResourceAdmin(){
        try{
            $data = ShopService::instance()->PurchaseResourceAdmin();

                return $this->view("shop.shop_user_purchase_resource_admin")->with('data',$data);

        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
        return $this->view("shop.shop_user_purchase_resource_admin");
    }
    /**
     * @return mixed
     * 个人中心采购单删除
     */
    public function UserPurchaseResourceDel(){
        $paramObj = new stdClass();
        $paramObj->purchase_id = $this->getParam('purchase_id');   //id
        $id= ShopService::instance()->UserPurchaseResourceDel($paramObj->purchase_id);
        if($id = 1){

            try{
                $data = ShopService::instance()->PurchaseResourceAdmin();
                if($data){
                    return $this->view("shop.shop_user_purchase_resource_admin")->with('data',$data);
                }
            }catch(Exception $e){
                $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
            }


        }else{

            return $this->view("shop.shop_user_purchase_resource_admin");
        }






    }

    /**
     * @return mixed
     */
    public function UserReceiptAddress(){
        $paramObj = new stdClass();
        $paramObj->user_id =session_get(UserEnum::USER_INFO_USER_ID);   //user_id
        try{
            $data = ShopService::instance()->UserReceiptAddress($paramObj->user_id);
                return $this->view("shop.shop_user_receipt_address")->with('data',$data);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }

        return $this->view("shop.shop_user_receipt_address");
    }

    /**
     * @return mixed
     * 商家入驻
     */
    public function UserSellerJoin(){
        $status = ShopService::instance()->GetSupplierStatus();
        return $this->view("shop.shop_user_seller_join")->with('status',$status);
    }


    /**
     * @return mixed
     * 商家入驻
     */
    public function UserSellerJoinEstablishment(){
        $paramObj                         =          new stdClass();
        $paramObj->user_id                =            session_get(UserEnum::USER_INFO_USER_ID);
        $paramObj->invite_code            =          $this->getParam('invite_code');         //邀请码
        $status = ShopService::instance()->UserSellerJoinEstablishment($paramObj);
        return $status;
    }

    /**
     * @return mixed
     * 商家重新入住
     */
    public function UpdateUserSellerJoin(){
        $status = 3;
        $user_id                  =            session_get(UserEnum::USER_INFO_USER_ID);
        $seller_info = ShopService::instance()->GetSupplierInfo($user_id);
        return $this->view("shop.shop_user_seller_join")->with(['status'=>$status,'info'=>$seller_info]);
    }


    /**
     * @return mixed
     * 商家入驻信息入库
     */
    public function UserSellerJoinInfo(){
        #接受参数
        $paramObj                               =          new stdClass();
        $paramObj->supplier_id                  =                  $this->getParam('supplier_id');
        $paramObj->user_id                      =            session_get(UserEnum::USER_INFO_USER_ID);
        $paramObj->supplier_name                =          $this->getParam('supplier_name');            //商家名称
        $paramObj->company                      =          $this->getParam('supplier_name');            //公司名称
        $paramObj->address                      =          $this->getParam('address');                  //公司地址
        $paramObj->duty_name                    =          $this->getParam('duty_name');                //负责人名称
        $paramObj->duty_mobile                  =          $this->getParam('duty_mobile');              //负责人电话
        $paramObj->operate_license              =          $this->getParam('operate_license');          //营业执照
        $paramObj->organization                 =          $this->getParam('organization');             //组织机构代码证
        $paramObj->tax_license                  =          $this->getParam('tax_license');              //税务登记证
        $paramObj->account_licence              =          $this->getParam('account_licence');//开户许可证
        $status = ShopService::instance()->UserSellerJoinInfo($paramObj);
        if($status){
            return Redirect::to('/user_seller_join');
        }

    }


    /**
     * @return mixed
     */
    public function UserAmend_pass(){
        return $this->view("shop.shop_user_amend_pass")->with('user_id',session_get(UserEnum:: USER_INFO_USER_ID));
    }
    /**
     * 查询密码是否正确
     */
    public function UserPasswordSelect()
    {
        $paramObj = new stdClass();
        $paramObj->user_id = $this->getParam('user_id');   //id
        $paramObj->old_password = $this->getParam('password');   //id

        $id = ShopService::instance()->UserPasswordSelect($paramObj->user_id, $paramObj->old_password);
        if($id == 1){
            $rs['code']=1;
        }else{
            $rs['code']=0;
        }
        return $rs;
    }
    public function UserPasswordUpdate(){
        $paramObj = new stdClass();
        $paramObj->user_id = $this->getParam('user_id');   //id
        $paramObj->old_password = $this->getParam('old_password');   //id
        $paramObj->new_password = $this->getParam('new_password');   //id


        $id = ShopService::instance()->UserPasswordSelect($paramObj->user_id, $paramObj->old_password);
        if($id == 1){
            $ids= ShopService::instance()->UserPasswordUpdate($paramObj->new_password,$paramObj->user_id );
            if($ids==1){
                $rs['code']=1;
            }else{
                $rs['code']=0;
            }
        }else{
            $rs['code']=0;
        }
        return $rs;
    }
    public function UserOrder(){
        $paramObj = new stdClass();
        $paramObj->user_id           =   session_get(UserEnum::USER_INFO_USER_ID);      //用户ID
        $paramObj->status           =   $this->getParam('status');      //订单状态（买家：0,全部,1，待付款，2，待发货，3，待收货)
        try{
            $data = ShopService::instance()->GetOrderAllPc($paramObj->user_id ,$paramObj->status );
            return $this->view("shop.shop_user_order") ->with('data',$data);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }

        return $this->view("shop.shop_user_order");

    }
    public function UserOrderNum(){
        $paramObj = new stdClass();
        $paramObj->user_id           =   session_get(UserEnum::USER_INFO_USER_ID);      //用户ID
        $paramObj->status           =   $this->getParam('status');      //订单状态（买家：0,全部,1，待付款，2，待发货，3，待收货)
        $data = ShopService::instance()->GetOrderAllPc($paramObj->user_id ,$paramObj->status );
        $num=count($data);
        $rs['code']=$num;
        return $rs;

    }
    //订单取消
    public function CancelOrderPc()
    {
        #收集信息
        //$paramObj                    =   new stdClass();
        $paramObj = new stdClass();
        $paramObj->order_id           =   $this->getParam('order_id');
        //库存恢复
        $kc = ApiService::instance()->kcback( $paramObj->order_id);
        ApiService::instance()->kcupdate($kc->product_id,$kc->number);
        #逻辑处理
        $data = ApiService::instance()->CancelOrder( $paramObj->order_id);
        if($data==1){
            $rs['code']=1;
        }else{
            $rs['code']=0;
        }
        return $rs;

    }
    //订单取消
    public function DelOrderPc()
    {
        #收集信息
        //$paramObj                    =   new stdClass();
        $paramObj = new stdClass();
        $paramObj->order_id           =   $this->getParam('order_id');
        $data = ShopService::instance()->DeleteOrder($paramObj->order_id );
        if($data==1){
            $rs['code']=1;
        }else{
            $rs['code']=0;
        }
        return $rs;
    }

    //确认收货
    public function ConfirmReceipt(){
        $paramObj = new stdClass();
        $paramObj->order_id           =   $this->getParam('order_id');

        #逻辑处理
         $data = ApiService::instance()->ConfirmReceipt($paramObj->order_id);
        if($data==1){
            $rs['code']=1;
        }else{
            $rs['code']=0;
        }
        return $rs;
    }
    public function OrderDetails(){

        return $this->view("shop.shop_user_order_details");
    }
    //订单详情
    public function OrderDetailPc()
    {
        #收集信息
        $paramObj = new stdClass();
        $paramObj->order_id = $this->getParam('order_id');

        #逻辑处理

        $data = ShopService::instance()->OrderDetail($paramObj->order_id);

        return $this->view("shop.shop_user_order_details")->with('data', $data);

    }
    /**
     * 添加购物车
     * @author fyl
     * @data  2016-7-01
     */
    public function ShopAddCart()
    {
        #收集信息
        $paramObj                    =   new stdClass();
        $paramObj->product_id        =   $this->getParam('product_id');
        $paramObj->number            =   $this->getParam('number');
        #逻辑处理
        try{
            $data = ShopService::instance()->ShopAddCart($paramObj);
            return $data;
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * 购物车列表
     * @author fyl
     * @data  2016-7-01
     */
    public function ShopCartList()
    {
        #逻辑处理
        try{
            $data = ShopService::instance()->ShopCartList();
            $count = ShopService::instance()->ShopCartCount();
            return $this->view('shop.shop_user_cart_list')->with(['data'=>$data,'count'=>$count]);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * 删除购物车商品
     * @author fyl
     * @data  2016-7-02
     */
    public function ShopCartDelete()
    {
        #接受参数
        $paramObj                     =          new stdClass();
        $paramObj->cart_id           =          $this->getParam('cart_id');         //现货ID
        $list = ShopService::instance()->ShopCartDelete($paramObj);
        $this->rest->success(['state' => $list], ErrorCodeEnum::STATUS_SUCCESS, '刪除成功！');

    }

    /**
     * 购物车更新商品
     * @author fyl
     * @data 2016-7-8
     */
    public function ShopUpdateCart()
    {
        #收集信息
        $paramObj                    =   new stdClass();
        $paramObj->product_id        =   $this->getParam('product_id');
        $paramObj->product_num       =   $this->getParam('product_num');

       return ShopService::instance()->ShopUpdateCart($paramObj);

    }

    /**
     * 删除购物车商品
     * @author fyl
     * @data  2016-7-02
     */
    public function HeadCartDelete()
    {
        #接受参数
        $paramObj                     =          new stdClass();
        $paramObj->cart_id           =          $this->getParam('cart_id');         //现货ID
        $list = ShopService::instance()->HeadCartDelete($paramObj);
        if($list){
            return Redirect::back();
        }

    }


    /**
     * 购物车删除ajax返回模板
     * @return mixed
     * @author fyl
     * @date 2016-06-29
     */
    public function ShopCartAjax()
    {
        $info = ShopService::instance()->ShopCartList();
        $count = ShopService::instance()->ShopCartCount();
        return $this->viewAjax('shop.ajaxTemplates.shop_cart_part')->with(['data'=>$info,'count'=>$count]);
    }

    /**
     * 提交购物车
     * @return mixed
     * @author fyl
     * @date 2016-06-29
     */
    public function ShopCartSubmit()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $cart_id           =   $this->getParam('cart_id');
        $cart_info = ShopService::instance()->ShopCartInfo($user_id,$cart_id);
        if(empty($cart_info)){
         return Redirect::to('/user_order?status=0');
        }
        $cart_info->cart_id = $cart_id;
        return $this->view('shop.shop_user_cart_submit')->with('cart_info',$cart_info);
    }

    /**
     * 生成订单
     * @author fyl
     * @date 2016-7-8
     */
    public function ShopGenerateOrder()
    {
        #收集信息
        $paramObj                      =   new stdClass();
       // $paramObj->address_id          =   $this->getParam('address_id');
        $paramObj->pay                         =   $this->getParam('pay');
        $paramObj->cart_id             =   $this->getParam('cart_id');

        #逻辑处理
        try{
            $order_id =  ShopService::instance()->ShopGenerateOrder($paramObj);
            if(empty($order_id)){
                return Redirect::to('/user_order?status=0');
            }
            $this->JdPayFirst($order_id);
            return $this->view('shop.shop_user_cashier');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }
    }

    /**
     * @return mixed
     * 订单支付
     */
    public function JdPayFirst($order_id){
        $data = ApiService::instance()->SelectOrder($order_id );
        $data['order_id']=$order_id;
        $_SESSION['array'] = $data;
        // $config = Config::get("jd_pay");
        // $serverPayUrl = $config["serverPayUrl"];

    }

    /*
     * 设为默认地址
     */
    public function DefaultAddress(){
        $paramObj = new stdClass();
        $paramObj->address_id           =   $this->getParam('address_id');

        #逻辑处理
        $data = ApiService::instance()->ModifyIsDefault1($paramObj->address_id);

        if($data['is_default']==1){
            $rs['code']=1;
        }else{
            $rs['code']=0;
        }
        return $rs;
    }
    //删除收货地址
    public function DeleteAddress(){
        $paramObj                   =          new stdClass();
        $paramObj->address_id       =          $this->getParam('address_id');               //地址ID
        $paramObj->is_default       =         0;               //是否默认地址

        #逻辑处理
        $data = ApiService::instance()->DeleteAddress($paramObj);
        if($data->status==1){
            $rs['code']=1;
        }else{
            $rs['code']=0;
        }
        return $rs;



    }
    //修改查询
    public function UpdateSelect(){
        $paramObj                   =          new stdClass();
        $paramObj->address_id       =          $this->getParam('address_id');               //地址ID
        #逻辑处理
        $data = ShopService::instance()->UpdateSelect($paramObj->address_id);
        if($data){
            $rs['code']=1;
            $rs['address_id']=$data->address_id;
            $rs['consignee']=$data->consignee;
            $rs['mobile']=$data->mobile;
            $rs['address']=$data->address;
            $rs['address_all']=$data->address_all;

        }else{
            $rs['code']=0;
        }
        return $rs;



    }

    public function UpdateAddress(){
        $paramObj                   =          new stdClass();
        $paramObj->address_id       =          $this->getParam('address_id');               //地址ID
        $paramObj->is_default       =          $this->getParam('is_default');
        $paramObj->consignee       =          $this->getParam('consignee');
        $paramObj->mobile       =          $this->getParam('mobile');
        $paramObj->address_all       =          $this->getParam('address_all');
        $paramObj->address       =          $this->getParam('address');
        $data = ShopService::instance()->ModifyAddress($paramObj);
        if(!empty($data)){
            $rs['code']=1;

        }else{
            $rs['code']=0;
        }
        return $rs;

    }



}